package com.corx.network.core.manager;

import com.corx.network.core.storage.Storage;
import com.corx.network.core.type.StorageTypeEnum;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gx
 * @version 1.0.0
 */
public class StorageManager {


    private final Map<StorageTypeEnum, Storage> storageMap = new ConcurrentHashMap<>();

    public void registerStorage(Storage storage) {
        StorageTypeEnum type = storage.type();
        if (storageMap.containsKey(type)) {
            throw new IllegalStateException("消息存储类型[" + type + "]已注册.");
        }
        storageMap.put(type, Objects.requireNonNull(storage, "存储不能为空"));
    }

    public Storage getStorage(StorageTypeEnum type) {
        Storage storage = storageMap.get(type);
        if (storage == null) {
            throw new IllegalArgumentException("类型[" + type + "]的MessageStorage未注册。");
        }
        return storage;
    }


    public Set<StorageTypeEnum> supportedTypes() {
        return storageMap.keySet();
    }

    public void shutdown() {
        storageMap.clear();
    }
}
